package com.ssl.basics.sort;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author ssl
 * @create 2021/6/8 0008.
 */
public class Stream {

    static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) throws ParseException {
        List<User> list = loadList();
        list = sortByAgeAndCreateDate(list);
        System.out.println(list);
    }

    /**
     * 加载测试用的list
     *
     * @return
     * @throws ParseException
     */
    public static List<User> loadList() throws ParseException {
        List<User> users = new ArrayList<>();
        User user;
        for (int i = 1; i < 10; i++) {
            user = new User();
            user.setAge(Integer.parseInt("1" + i));
            user.setCreateDate(sf.parse("2021-06-0".concat(String.valueOf(i)).concat(" 00:00:00")));
            if (3 == i || 7 == i) {
                user.setDeleted(true);
            }
            if (5 == i) {
                user.setDeleted(false);
            }
            user.setName(String.valueOf(i));
            users.add(user);
        }
        return users;
    }

    public static List<User> sortByAgeAndCreateDate(List<User> list) {
        // 1、未删除的用户排在最前面(即deleted为true的排在最后,deleted字段可能为null)；
        // 2、年龄大的排前面；
        // 3、创建时间早的排前面
        list = list.stream()
                .sorted(
                        Comparator.comparing((User u) -> {
                            if (null != u.getDeleted() && u.getDeleted()) {
                                // 1:表示按正序，如果条件为true，则会符合条件的排在后面，
                                // -1：表示倒序，如果条件为true，则会将符合条件的排在前面；
                                // 0:表示不变
                                return 1;
                            }
                            return -1;
                        })
                        .thenComparing(User::getAge,Comparator.reverseOrder())
                        .thenComparing(User::getCreateDate,Comparator.naturalOrder())
                )
                .collect(Collectors.toList());

        return list;
    }


    static class User {
        private int age;
        private Date createDate;
        private String name;
        private Boolean deleted;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Date getCreateDate() {
            return createDate;
        }

        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Boolean getDeleted() {
            return deleted;
        }

        public void setDeleted(Boolean deleted) {
            this.deleted = deleted;
        }

        @Override
        public String toString() {
            return "User{" +
                    "age=" + age +
                    ", createDate=" + sf.format(createDate) +
                    ", name='" + name + '\'' +
                    ", deleted=" + deleted +
                    '}';
        }
    }
}
